package com.kingsoft.gtdsys.dao.impl.sqllite;

import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.kingsoft.gtdsys.util.Log;

public class DBOpenHelper extends SQLiteOpenHelper {
	public static final String DB_NAME = "gtd_db";
	public static final String STUFF_TABLE_NAME = "t_stuffs";
	public static final String TRASH_TABLE_NAME = "t_trashes";
	public static final String REFERENCE_TABLE_NAME = "t_references";
	public static final String MAYBE_TABLE_NAME = "t_maybes";
	public static final String ACTION_TABLE_NAME = "t_actions";
	public static final String WAITING_TABLE_NAME = "t_waitings";
	
	public static final int DB_VERSION = 3;
	
	private static final String DB_CREATE =
		"CREATE TABLE " + STUFF_TABLE_NAME
		+ " ("
		+		"_id INTEGER PRIMARY KEY,"
		+		"content TEXT NOT NULL,"
		+		"date TEXT"
		+	");"
		+ "CREATE TABLE " + TRASH_TABLE_NAME
		+ " (" 
		+		"_id INTEGER PRIMARY KEY," 
		+		"content TEXT NOT NULL," 
		+		"date TEXT);"
		+ "CREATE TABLE " + REFERENCE_TABLE_NAME
		+ " ("
		+		"_id INTEGER PRIMARY KEY,"
		+		"content TEXT NOT NULL,"
		+		"date TEXT,"
		+		"organized TEXT,"
		+		"type TEXT"
		+	");"
		+ "CREATE TABLE " + MAYBE_TABLE_NAME
		+ " (_id INTEGER PRIMARY KEY," +
				"content TEXT NOT NULL," +
				"date TEXT,"
		+		"organized TEXT,"
		+		"occasion TEXT);"
		+ "CREATE TABLE " + ACTION_TABLE_NAME
		+ " ("
		+		"_id INTEGER PRIMARY KEY,"
		+		"content TEXT NOT NULL,"
		+		"date TEXT,"
		+		"detail TEXT,"
		+		"importance TEXT,"
		+		"context TEXT,"
		+		"energy TEXT,"
		+		"planTime TEXT,"
		+		"needTime TEXT,"
		+		"completed TEXT"
		+	");"
		+ "CREATE TABLE " + WAITING_TABLE_NAME
		+ " (_id INTEGER PRIMARY KEY," 
		+		"content TEXT NOT NULL," 
		+		"date TEXT,"
		+		"contact TEXT);";
	public DBOpenHelper(Context context){
		super(context,DB_NAME, null, DB_VERSION);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		try {
			db.execSQL(DBOpenHelper.DB_CREATE);
		} catch (SQLException e) {
			Log.e(e.toString());
		}
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + STUFF_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + TRASH_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + REFERENCE_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + MAYBE_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + ACTION_TABLE_NAME);
		db.execSQL("DROP TABLE IF EXISTS " + WAITING_TABLE_NAME);
		this.onCreate(db);
	}
}